using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The FamilyMemberAddressTableDB class is responsible for interacting with the database to retrieve and store information
/// about FamilyMemberAddressTable objects.
/// </summary>
public class FamilyMemberAddressTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of FamilyMemberAddressTable from the underlying datasource.
/// </summary>
/// <param name="familyMemberAddressKey">The unique FamilyMemberAddressKey of the FamilyMemberAddressTable in the database.</param>
/// <returns>An FamilyMemberAddressTable when the FamilyMemberAddressKey was found in the database, or null otherwise.</returns>
public static FamilyMemberAddressTable GetItem(int familyMemberAddressKey)
{
FamilyMemberAddressTable myFamilyMemberAddressTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberAddressTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@familyMemberAddressKey", familyMemberAddressKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myFamilyMemberAddressTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myFamilyMemberAddressTable;
}
}


public static FamilyMemberAddressTable GetItemByUserAccountKey(int UserAccountKey)
{
    FamilyMemberAddressTable myFamilyMemberAddressTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberAddressSelectSingleItemByUserAccountKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myFamilyMemberAddressTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myFamilyMemberAddressTable;
    }
}

/// <summary>
/// Returns a list with FamilyMemberAddressTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberAddressTable objects.</returns>
public static FamilyMemberAddressTableList GetList()
{
FamilyMemberAddressTableList tempList = new FamilyMemberAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberAddressTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with FamilyMemberAddressTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberAddressTable objects.</returns>
public static FamilyMemberAddressTableList GetListByUserAccountKey(int UserAccountKey)
{
FamilyMemberAddressTableList tempList = new FamilyMemberAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberAddressTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a FamilyMemberAddressTable in the database.
/// </summary>
/// <param name="myFamilyMemberAddressTable">The FamilyMemberAddressTable instance to save.</param>
/// <returns>The new FamilyMemberAddressKey if the FamilyMemberAddressTable is new in the database or the existing FamilyMemberAddressKey when an item was updated.</returns>
public static int Save(FamilyMemberAddressTable myFamilyMemberAddressTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberAddressTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myFamilyMemberAddressTable.FamilyMemberAddressKey == -1)
{myCommand.Parameters.AddWithValue("@familyMemberAddressKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@familyMemberAddressKey", myFamilyMemberAddressTable.FamilyMemberAddressKey);
}
if (myFamilyMemberAddressTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myFamilyMemberAddressTable.UserAccountKey);
}
if (string.IsNullOrEmpty(myFamilyMemberAddressTable.UnitNo))
{
myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@unitNo", myFamilyMemberAddressTable.UnitNo);
}
if (string.IsNullOrEmpty(myFamilyMemberAddressTable.StreetName))
{
myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@streetName", myFamilyMemberAddressTable.StreetName);
}
if (myFamilyMemberAddressTable.CityCode == null){
myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@cityCode", myFamilyMemberAddressTable.CityCode);
}
if (myFamilyMemberAddressTable.DistrictCode == null){
myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtCode", myFamilyMemberAddressTable.DistrictCode);
}
if (myFamilyMemberAddressTable.TownshipCode == null){
myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@townshipCode", myFamilyMemberAddressTable.TownshipCode);
}
if (myFamilyMemberAddressTable.CountryCode == null){
myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@countryCode", myFamilyMemberAddressTable.CountryCode);
}
if (string.IsNullOrEmpty(myFamilyMemberAddressTable.PostalCode))
{
myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@postalCode", myFamilyMemberAddressTable.PostalCode);
}
if (myFamilyMemberAddressTable.AddressTypeCode == null){
myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@addressTypeCode", myFamilyMemberAddressTable.AddressTypeCode);
}
if (myFamilyMemberAddressTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myFamilyMemberAddressTable.Status);
}
if (myFamilyMemberAddressTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myFamilyMemberAddressTable.CreatedDateTime);
}
if (myFamilyMemberAddressTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myFamilyMemberAddressTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a FamilyMemberAddressTable from the database.
/// </summary>
/// <param name="familyMemberAddressKey">The FamilyMemberAddressKey of the FamilyMemberAddressTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int familyMemberAddressKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberAddressTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@familyMemberAddressKey", familyMemberAddressKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the FamilyMemberAddressTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static FamilyMemberAddressTable FillDataRecord(IDataRecord myDataRecord )
{
FamilyMemberAddressTable myFamilyMemberAddressTable = new FamilyMemberAddressTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FamilyMemberAddressKey")))
{
myFamilyMemberAddressTable.FamilyMemberAddressKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("FamilyMemberAddressKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myFamilyMemberAddressTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
{
myFamilyMemberAddressTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
{
myFamilyMemberAddressTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
{
myFamilyMemberAddressTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
{
myFamilyMemberAddressTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
{
myFamilyMemberAddressTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
{
myFamilyMemberAddressTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
{
myFamilyMemberAddressTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
{
myFamilyMemberAddressTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myFamilyMemberAddressTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myFamilyMemberAddressTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myFamilyMemberAddressTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myFamilyMemberAddressTable;
}
}

 } 
